package hydrap2p.helper;

import hydrap2p.servicemanager.NameTaken;

import java.io.IOException;
import java.net.BindException;

import org.apache.thrift.TProcessor;
import org.apache.thrift.transport.TTransportException;

public class ThriftUtils {

	public static final int CONNECTION_TIMEOUT = 10000;		//10 seconds in millis

	public static ThriftServerThread initThriftServer(final TProcessor serverType, final String name) throws BindException, IOException, NameTaken {
		return new ThriftServerThread(serverType, name, -1);
	}
	public static ThriftServerThread initThriftServer(final TProcessor serverType, final String name, final int defaultPort) throws BindException, IOException, NameTaken {
		return new ThriftServerThread(serverType, name, defaultPort);
	}
	
	public static boolean waitForLibrary() {
		long startTime = System.currentTimeMillis();
		long lastAttempt = System.currentTimeMillis();
		
		while(ServiceConnector.getLibrary() == null) {
			//Only try once per second
			if(System.currentTimeMillis() >= lastAttempt + 1000) {
				lastAttempt = System.currentTimeMillis();
			}
			
			if(System.currentTimeMillis() > startTime + CONNECTION_TIMEOUT) {
				System.err.println("ThriftUtils - Failed to connect to Library!");
				return false;
			}
		}
		
		return true;
	}
	
	public static boolean waitForManager() {
		long startTime = System.currentTimeMillis();
		long lastAttempt = System.currentTimeMillis();
		
		while(ServiceConnector.getServiceManager() == null) {
			
			if(System.currentTimeMillis() > startTime + CONNECTION_TIMEOUT) {
				System.err.println("ThriftUtils - Failed to connect to Manager!");
				return false;
			}
			else
			{
				//Only try once per second
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					System.err.println("ThriftUtils - Failed to connect to Manager!");
					return false;
				}
			}
		}
		
		return true;
	}
	
	public static boolean waitForLogging() {
		long startTime = System.currentTimeMillis();
		long lastAttempt = System.currentTimeMillis();
		
		while(ServiceConnector.getLogger() == null) {
			//Only try once per second
			if(System.currentTimeMillis() >= lastAttempt + 1000) {
				lastAttempt = System.currentTimeMillis();
			}
			
			if(System.currentTimeMillis() > startTime + CONNECTION_TIMEOUT) {
				System.err.println("ThriftUtils - Failed to connect to Logger!");
				return false;
			}
		}
		
		return true;
	}
	
	public static boolean waitForSearcher() {
		long startTime = System.currentTimeMillis();
		long lastAttempt = System.currentTimeMillis();
		
		while(ServiceConnector.getSearcher() == null) {
			//Only try once per second
			if(System.currentTimeMillis() >= lastAttempt + 1000) {
				lastAttempt = System.currentTimeMillis();
			}
			
			if(System.currentTimeMillis() > startTime + CONNECTION_TIMEOUT) {
				System.err.println("ThriftUtils - Failed to connect to Searcher!");
				return false;
			}
		}
		
		return true;
	}
	
	public static boolean waitForDownloader() {
		long startTime = System.currentTimeMillis();
		long lastAttempt = System.currentTimeMillis();
		
		while(ServiceConnector.getDownloader() == null) {
			//Only try once per second
			if(System.currentTimeMillis() >= lastAttempt + 1000) {
				lastAttempt = System.currentTimeMillis();
			}
			
			if(System.currentTimeMillis() > startTime + CONNECTION_TIMEOUT) {
				System.err.println("ThriftUtils - Failed to connect to Downloader!");
				return false;
			}
		}
		
		return true;
	}
}
